package com.hong.controller.system;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hong.common.AjaxResultVo;
import com.hong.common.anno.Log;
import com.hong.common.controller.BaseController;
import com.hong.entity.SysRole;
import com.hong.entity.SysUser;
import com.hong.entity.SysUserRole;
import com.hong.service.ISysDeptService;
import com.hong.service.ISysRoleService;
import com.hong.service.ISysUserRoleService;
import com.hong.service.ISysUserService;

@Controller
@RequestMapping("/system/user")
public class UserController extends BaseController {

	@Autowired
	private ISysUserService sysUserService;

	@Autowired
	private ISysRoleService sysRoleService;

	@Autowired
	private ISysUserRoleService sysUserRoleService;

	@Autowired
	private ISysDeptService sysDeptService;

	/**
	 * 跳转用户管理
	 * 
	 * @return
	 */
	@RequiresPermissions("listUser")
	@RequestMapping("/listPage")
	public String listPage() {
		return "system/user/list";
	}

	/**
	 * 分页查询用户
	 * 
	 * @param search    检索条件
	 * @param daterange 时间范围筛选
	 * @param limit     每页条数
	 * @param offset    偏移值
	 * @return
	 */
	@RequiresPermissions("listUser")
	@RequestMapping("/listUser")
	@ResponseBody
	public AjaxResultVo listUser(String search, String daterange, Integer limit, Integer offset) {
		PageHelper.startPage(getPageNo(limit, offset), limit);
		List<Map<Object, Object>> userList = sysUserService.selectUserPage(search);
		PageInfo<Map<Object, Object>> pageData = new PageInfo<>(userList);
		AjaxResultVo resultVo = AjaxResultVo.success();
		resultVo.put("total", pageData.getTotal());
		resultVo.put("rows", pageData.getList());
		return resultVo;
	}

	/**
	 * 跳转用户添加
	 * 
	 * @param model
	 * @return
	 */
	@RequiresPermissions("addUser")
	@RequestMapping("/add")
	public String add(Model model) {
		model.addAttribute("roleList", sysRoleService.list(null));
		model.addAttribute("deptList", sysDeptService.list(null));
		return "system/user/add";
	}

	/**
	 * 执行用户新增
	 * 
	 * @param user
	 * @param roleId
	 * @return
	 */
	@Log("创建用户")
	@RequiresPermissions("addUser")
	@RequestMapping("/doAdd")
	@ResponseBody
	public AjaxResultVo doAdd(SysUser user, @RequestParam(value = "roleId[]", required = false) String[] roleId) {
		sysUserService.insertUser(user, roleId);
		return AjaxResultVo.success();
	}

	/**
	 * 编辑用户
	 * 
	 * @param id
	 * @param model
	 * @return
	 */
	@RequestMapping("/edit/{id}")
	@RequiresPermissions("editUser")
	public String edit(@PathVariable String id, Model model) {
		SysUser sysUser = sysUserService.getById(id);
		List<SysRole> sysRoles = sysRoleService.list(null);
		QueryWrapper<SysUserRole> ew = new QueryWrapper<SysUserRole>();
		ew.eq("user_id ", id);
		List<SysUserRole> mySysUserRoles = sysUserRoleService.list(ew);
		List<String> myRolds = mySysUserRoles.stream().map(m -> m.getRoleId()).collect(Collectors.toList());
		model.addAttribute("sysUser", sysUser);
		model.addAttribute("sysRoles", sysRoles);
		model.addAttribute("myRolds", myRolds);
		model.addAttribute("deptList", sysDeptService.list(null));
		return "system/user/edit";
	}

	/**
	 * 执行编辑
	 * 
	 * @param sysUser
	 * @param roleId
	 * @return
	 */
	@Log("编辑用户")
	@RequiresPermissions("editUser")
	@RequestMapping("/doEdit")
	@ResponseBody
	public AjaxResultVo doEdit(SysUser sysUser, @RequestParam(value = "roleId[]", required = false) String[] roleId) {
		sysUserService.updateUser(sysUser, roleId);
		return AjaxResultVo.success();
	}

	/**
	 * 删除用户
	 * 
	 * @param id
	 * @return
	 */
	@Log("删除用户")
	@RequiresPermissions("deleteUser")
	@RequestMapping("/delete/{id}")
	@ResponseBody
	public AjaxResultVo delete(@PathVariable String id) {
		if(id.equals(getUserId())) {
			return AjaxResultVo.error("当前用户无法删除");
		}
		sysUserService.delete(id);
		return AjaxResultVo.success();
	}

	/**
	 * 验证用户名是否存在
	 * 
	 * @param userName
	 * @return
	 */
	@RequestMapping("/checkName")
	@ResponseBody
	public AjaxResultVo checkName(String userName) {
		List<SysUser> list = sysUserService.list(new QueryWrapper<SysUser>().eq("user_name", userName));
		if (list.size() > 0) {
			return AjaxResultVo.error("用户名已存在");
		}
		return AjaxResultVo.success();
	}

}
